/**
 *    Copyright 2012 Geensoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.jeecommons.web.jsf.util;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.IndexedColors;

/**
 *
 * @author Jorge E. Villaverde
 * @since 1.0.0
 * @version 1.0.0
 */
public abstract class XlsUtil implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7432972625206359793L;

	private static final DateFormat dateFrmt = new SimpleDateFormat("dd/MM/yyyy");

	public static HSSFCellStyle createHeaderStyle(HSSFWorkbook wb){
        // Header font
        HSSFFont headerFont = wb.createFont();
        headerFont.setFontHeightInPoints((short)10);
        headerFont.setFontName("Arial");
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        
        HSSFCellStyle headerStyle = wb.createCellStyle();
        headerStyle.setWrapText(true);
        headerStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
        headerStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        headerStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
        headerStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        headerStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
        headerStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
        headerStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
        headerStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
        headerStyle.setFont(headerFont);

        return headerStyle;
	}

	public static HSSFCellStyle createCellStyle(HSSFWorkbook wb){
        HSSFFont cellFont = wb.createFont();
        cellFont.setFontHeightInPoints((short)10);
        cellFont.setFontName("Arial");
        cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setFont(cellFont);
        
        return cellStyle;
	}

	public static HSSFCell addCell(HSSFRow row, int cellNumber, Date value) {
        HSSFCell cell = row.createCell(cellNumber);
		if(value != null){
			cell.setCellValue(dateFrmt.format(value));
		}
        return cell;
	}
	
	public static HSSFCell addCell(HSSFRow row, int cellNumber, String value) {
        HSSFCell cell = row.createCell(cellNumber);
        cell.setCellValue(value);
        return cell;
	}
	
	public static HSSFCell addCell(HSSFRow row, int cellNumber, int value) {
        HSSFCell cell = row.createCell(cellNumber);
        cell.setCellValue(value);
		return cell;
	}

	public static HSSFCell addCell(HSSFRow row, int cellNumber, boolean value) {
        HSSFCell cell = row.createCell(cellNumber);
        cell.setCellValue(value);
		return cell;		
	}
	
	public static HSSFCell addCell(HSSFRow row, int cellNumber, double value) {
        HSSFCell cell = row.createCell(cellNumber);
        cell.setCellValue(value);
		return cell;		
	}
	
	public static HSSFCell addCell(HSSFRow row, int cellNumber, long value) {
        HSSFCell cell = row.createCell(cellNumber);
        cell.setCellValue(value);
		return cell;		
	}
	
	public static HSSFCell addCellFormula(HSSFRow row, int cellNumber, String formula) {
        HSSFCell cell = row.createCell(cellNumber);
        cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
        cell.setCellFormula(formula);        
		return cell;						
	}
	
	public static HSSFCell createCell(HSSFWorkbook wb, HSSFRow row, int cellNumber, String value, short halign, short valign) {
		HSSFCell cell = row.createCell(cellNumber);
		cell.setCellValue(value);
		HSSFCellStyle cellStyle = wb.createCellStyle();
		cellStyle.setAlignment(halign);
		cellStyle.setVerticalAlignment(valign);
		cell.setCellStyle(cellStyle);		
		return cell;
	}	
}
